package com.hw.one.collect.dust.job;

import com.hw.one.collect.client.ClientFactory;
import com.hw.one.collect.dust.constans.SjwConstants;
import com.hw.one.collect.dust.dao.RaiseDustDao;
import com.hw.one.collect.dust.po.RaiseDust;
import com.hw.one.collect.dust.po.SiteInfo;
import com.hw.one.collect.dust.redis.BaseRedis;
import com.hw.one.collect.dust.util.WeatherUtil;
import com.hw.one.collect.spadata.po.MissData;
import com.hw.one.collect.util.DateUtil;
import com.hw.one.collect.util.MathCalculateUtil;
import com.hw.one.collect.util.SerializeUtil;
import com.mlb.analyse.webservice.DmDataInfoVO;
import com.mlb.analyse.webservice.GetPmData;
import org.apache.cxf.endpoint.Client;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/8/7.
 */
@Component
public class RaiseDustMissDataJob extends RaiseDustBaseJob{

    private static Logger log = Logger.getLogger(RaiseDustMissDataJob.class);

    @Resource
    private BaseRedis baseRedis;

    @Resource
    private RaiseDustDao raiseDustDao;


    public void start() {


        // 查询出来需要计算的数据，格式
        // siteCode|时间|次数
        log.info("----------------查询缺失记录----");
        List<MissData> missDataList = baseInfoService.getMissData();
        log.info("------------本次查询：" + missDataList.size() + "个");
        if (missDataList.size() < 1) {
            log.info("----------------未查询到数据，跳过");
            return;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        for (MissData missData : missDataList) {
            try {
                // 先删除记录
                baseInfoService.deleteMissDataByValue(SerializeUtil.ObjTOSerialize(missData));

                String siteCode = missData.getSITECODE();
                String monitorTime = missData.getMONITORTIME();
                Integer count = missData.getCount();
                // 缺失数据的时间
                calendar.setTime(sdf.parse(monitorTime));
                // 时间取时间到之后的5分钟
                calendar.add(Calendar.MINUTE, 5);
                String endTime = sdf.format(calendar.getTime());
                Date date = sdf.parse(monitorTime);
                // 组装参数
                GetPmData getPmData = new GetPmData();
                String regionCode = baseInfoService.getRegionCodeBySiteCode(siteCode);
                // 发送请求必须要区域编码，如果未查到区域编码就跳过
                if (regionCode == null) {
                    log.info("-------------------未找到区域编码：" + missData);
                    continue;
                }
                // 组装数据
                getPmData.setDistrictCode(baseRedis.get("regionCode" + siteCode));
                getPmData.setBeginTime(monitorTime);
                getPmData.setEndTime(endTime);
                getPmData.setId(siteCode);

                List<RaiseDust> raiseDustList = executeGetRealTimeData(SjwConstants.getHistory, getPmData);
                boolean flag = true;
                if (null != raiseDustList && raiseDustList.size() > 0) {
//                    raiseDustDao.saveRaiseDustData(raiseDustList.get(0));
                    baseInfoService.insertLastData(raiseDustList.get(0).getSITECODE(), raiseDustList.get(0));
                    flag = false;
                }
                if (flag && new Date().getTime() - date.getTime() < 1000 * 60 * 60 * 2) {
                    count++;
                    if (count < 3) {
                        // 未查到数据
                        missData.setCount(++count);
                        baseInfoService.addMissData(SerializeUtil.ObjTOSerialize(missData));
                        log.info("------------------未查到数据，更新记录--" + missData.toString());
                    }
                }
            }catch (Exception e) {
                e.printStackTrace();
                baseInfoService.deleteMissDataByValue(SerializeUtil.ObjTOSerialize(missData));
                log.error("------------------数据异常，删除记录--" + missData.getSITECODE().toString());
            }

        }
    }
}
